package gov.kqqzyyy.monkey.orderpad.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import gov.kqqzyyy.monkey.orderpad.R;
import gov.kqqzyyy.monkey.orderpad.mytools.DateTimeUtil;
import gov.kqqzyyy.monkey.orderpad.object.DD_DDMX;
import gov.kqqzyyy.monkey.orderpad.object.post_param.DD_CPMX;

/**
 * Created by Monkey on 2017/9/11.
 * 历史订单适配器
 */

public class OrderDetailAdapter extends BaseAdapter {

    private final LayoutInflater inflater;
    private List<DD_DDMX> list;

    public OrderDetailAdapter(Context context) {
        inflater = LayoutInflater.from(context);
    }

    public void setList(List<DD_DDMX> list) {
        Collections.sort(list, (arg0, arg1) -> arg0.getDdh().compareTo(arg1.getDdh()));
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        if (list != null && list.get(i) != null) {
            return list.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.item_order_check, viewGroup, false);
            holder.ddh_tv = view.findViewById(R.id.ddh_tv);
            holder.scsj_tv = view.findViewById(R.id.scsj_tv);
            holder.sdcp_tv = view.findViewById(R.id.sdcp_tv);
            holder.zje_tv = view.findViewById(R.id.zjg_tv);
            holder.xdsj_tv = view.findViewById(R.id.xdsj_tv);
            holder.ddzt_tv = view.findViewById(R.id.ddzt_tv);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        if (list != null && list.size() > 0) {
            DD_DDMX orderDetail = list.get(i);
            if (orderDetail != null) {
                String ddh = orderDetail.getDdh();
                holder.ddh_tv.setText(ddh);
                Calendar calendar = Calendar.getInstance();
                Date xdsj = DateTimeUtil.getDFormatDate(orderDetail.getXdsj());
                calendar.setTime(Objects.requireNonNull(xdsj));
                calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE) + 1);
                String scsj = DateTimeUtil.getDFFormatString(calendar.getTime());
                scsj += "\n" + orderDetail.getScsj();
                int cc = Character.getNumericValue(ddh.charAt(ddh.length() - 2));
                switch (cc) {
                    case 1:
                        scsj += "\n" + "早餐";
                        break;
                    case 2:
                        scsj += "\n" + "午餐";
                        break;
                    case 3:
                        scsj += "\n" + "晚餐";
                        break;
                }
                holder.scsj_tv.setText(scsj);
//                Gson gson = new Gson();
//                List<SelectedDish> dishList = gson.fromJson(orderDetail.getSdcp(), new TypeToken<List<SelectedDish>>() {
//                }.getType());
                List<DD_CPMX> dishList = orderDetail.getSdcpList();
                StringBuilder cpsl = new StringBuilder();
                if (dishList != null) {
                    for (DD_CPMX dishInfo : dishList
                    ) {
                        if (cpsl.toString().equals("")) {
                            cpsl.append(dishInfo.getCPMC()).append("*").append(dishInfo.getCPSL());
                        } else {
                            cpsl.append("\n").append(dishInfo.getCPMC()).append("*").append(dishInfo.getCPSL());
                        }
                    }
                }
                holder.sdcp_tv.setText(cpsl.toString());
                holder.zje_tv.setText(orderDetail.getZjg());
                holder.xdsj_tv.setText(orderDetail.getXdsj());
                String fkfs;
                if (orderDetail.getDdzt().equals("2")) {//订单已经作废
                    fkfs = "订单已取消";
                } else {
                    if (orderDetail.getDdzt().equals("1")) {
//                        fkfs = "已付款\n交易号:" + orderDetail.getSHDDH();
                        fkfs = "已付款";
                    } else {
                        fkfs = "未付款";
                    }
                }

                holder.ddzt_tv.setText(fkfs);
            }

        }
        return view;
    }

    private static class ViewHolder {
        private TextView ddh_tv;
        private TextView scsj_tv;
        private TextView sdcp_tv;
        private TextView zje_tv;
        private TextView xdsj_tv;
        private TextView ddzt_tv;
    }
}
